package com.matchmealpha;

import java.util.List;
import java.util.Scanner;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class RecentActivity extends Activity {
	
	public static RecentActivity ACTIVE_INSTANCE; 
    public static boolean CURRENTLY_DOWNLOADING; 

	private Button leftButton;
	private Button rightButton;
	private TextView errorMsg;
	private RelativeLayout loadingPanel;

	private ListView recentLayout;
	private List<Post> recentList;
	private RecentListAdapter recentAdapter;
	

	private String username;
	private String sessionid;

	protected ProgressDialog loadingDialog;

	private int listingOrder;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		ACTIVE_INSTANCE = this;
		super.onCreate(savedInstanceState);
		/*** Shared Pref ***/
		SharedPreferences settings = getSharedPreferences(
				"matchme_preferences", 0);
		username = settings.getString("username", "");
		sessionid = settings.getString("sessionid", "");

		/*** Init ***/
		initLayout();

		/*** List View ***/
		recentList = MatchMeTabWidget.recentList;
		MatchMeTabWidget.recentAdapter = new RecentListAdapter(this, MatchMeTabWidget.recentList, 1);
		recentLayout.setAdapter(MatchMeTabWidget.recentAdapter);
		
		

//		if(recentList == null)
//		{
//			MatchMeTabWidget.recentList = new ArrayList<Post>();
//			recentList = MatchMeTabWidget.recentList;
//			recentAdapter = new RecentListAdapter(this, recentList, 1);
//			recentLayout.setAdapter(recentAdapter);
//			/*** Loading Screen ***/
//			loadingDialog = ProgressDialog.show(getParent(), "", "Connecting...",
//					true);
//			loadingDialog.show();
//			new RecentTask().execute();
//			
//		}else
//		{
//			recentAdapter = new RecentListAdapter(this, recentList, 1);
//			recentLayout.setAdapter(recentAdapter);
//		}
	}

	private void initLayout() {
		listingOrder = 0;
		setContentView(R.layout.recent_view);
		leftButton = (Button) this.findViewById(R.id.RecentLeftButton);
		rightButton = (Button) this.findViewById(R.id.RecentRightButton);
		recentLayout = (ListView) this.findViewById(R.id.RecentListView);
		errorMsg = (TextView) this.findViewById(R.id.RecentError);
		loadingPanel = (RelativeLayout) findViewById(R.id.RecentViewLoadingPanel);
		
		if(!CURRENTLY_DOWNLOADING){
			 loadingPanel.setVisibility(View.GONE);
		}
		

		leftButton.setVisibility(View.INVISIBLE);
		rightButton.setVisibility(View.INVISIBLE);
		errorMsg.setVisibility(View.GONE);

	}

	@Override
	protected void onResume() {
		super.onResume();
		//recentAdapter.notifyDataSetChanged();
	}

	private class RecentTask extends AsyncTask<String, Void, Boolean> {

		protected Boolean doInBackground(String... data) {

			HttpRequest req = new HttpRequest(sessionid);
			String response;
			response = req.sendPost(MatchMeTabWidget.MM_SERVER + "/get_recent/"
					+ listingOrder, "");
			if (MatchMeTabWidget.USER_AUTH_FAILURE.equals(response)
					|| MatchMeTabWidget.REQUEST_FAILURE.equals(response))
				return false;
			else if (response != null) {
				if (response != "") {
					Scanner in = new Scanner(response).useDelimiter(";");
					Post post;
					recentList.clear();
					while (in.hasNext()) {
						post = new Post();
						post.setPostID(in.nextInt());
						post.setUserID1(in.next());
						post.setUserID2(in.next());
						post.setLocation1(in.next());
						post.setLocation2(in.next());
						post.setStatus(in.next());
						post.setTime1(in.next());
						post.setTime2(in.next());
						post.setDescription1(in.next());
						post.setDescription2(in.next());
						//ShareActivity.getPostImage(post,sessionid);
						ShareActivity.getPostBitMapImage(post,sessionid);
						recentList.add(post);
					}
				}
				return true;
			} else
				return false;
		}

		
		protected void onPostExecute(Boolean result) {
			loadingDialog.cancel();
			if (result) {
				if (recentList.size() <= 0) {
					Toast.makeText(getParent(), "No Recent Images Found",
							Toast.LENGTH_LONG);
				}
			} else {
				errorMsg.setText("Unable to connect. Please try again");
			}
		}
		// leftButton.setVisibility(View.INVISIBLE);
		// rightButton.setVisibility(View.INVISIBLE);
	}

	public void hideLoadingPanel() {
		loadingPanel.setVisibility(View.GONE);	
	}
	
	public void showLoadingPanel() {
		loadingPanel.setVisibility(View.VISIBLE);	
	}
}
